<?php
class PointModule{
	function index(){
		global $_G;
		$action = isset($_GET['a'])?$_GET['a']:null;
		$lang = lang::load('point');
		loadialog();
		switch ($action){
			case 'rule'://获取积分的规则
				$rules = $_G['db']->fetch_all("SELECT * FROM ".DB_PREFIX."point_rule");
				break;
			case 'recharge'://充值
				if(!IS_POST){
					if(!$_G['settings']['point']['pointuse'] || $_G['settings']['point']['pointratio']<1){
						msg('暂未开启购买积分功能');
					}
					$ratio = 1/$_G['settings']['point']['pointratio'];
					$setting = unserialize(decrypt($_G['settings']['point']['pointsetting']));
					
					foreach($_G['settings']['payment'] as $k=>$v){
						$payments[$k]=unserialize(decrypt($v));
					}
				}else{
					if(!$_G['settings']['point']['pointuse'] || $_G['settings']['point']['pointratio']<1){
						msg('暂未开启购买积分功能');
					}
					$ratio = 1/$_G['settings']['point']['pointratio'];
					
					if(getGP('recharge_num','P','int')<1 && !getGP('recharge_numo','P','int')){
						error($lang['input_recharge_num']);
					}
					if(getGP('recharge_num','P','int')<1){
						$num = getGP('recharge_numo','P','int');
					}else{
						$num = getGP('recharge_num','P','int');
					}
					if(!getGP('payment','P')) {
						error($lang['choose_payment_type']);
					}
					$txn = array(
						'uid' => $_G['uid'],
						'type' => 'pointpay',
						'mail'	=> $_G['users']['email'],
						'gross'	=> $ratio * $num,
						'payment_type'	=> getGP('payment','P'),
						'amount'	=> $num,
						'created'	=> TIMESTAMP,
					);
					$txn['txnid']=$_G['db']->insert("orders",$txn);
					$txn['subject'] = $lang['buy'].$num.$lang['point'].'['.$_G['settings']['sitename'].']';
					
					m('payment');
					PaymentModule::payurl($txn,$txn['payment_type']);
				}
				
				break;
			case 'service'://使用积分开通服务
				
				break;
			case 'gotopay':
				if(!IS_POST){
					include template('uc/payment_ajax');
					exit;
				}else{
					$txnid = getGP('id','G','int');
					$paytype = getGP('payment','P');
					$_G['point']['recharge'] = 1;
					if(!$txnid || !$paytype){
						exit('<p align="center">'.$lang['payment_error'].'</p><p align="center"><a href="'.url('home.php?f=point&a=gotopay&ajax=1&id='.$txnid).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
					}
					$_G['db']->update('orders',array('payment_type'=>$paytype),array('txnid'=>$txnid));
					$txn = $_G['db']->fetch('orders','*',array('txnid'=>$txnid));
					if($txn['txnid'] != $txnid){
						exit('<p align="center">'.$lang['payment_error'].'</p><p align="center"><a href="'.url('home.php?f=point&a=gotopay&ajax=1&id='.$txnid).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
					}
					if($txn['status']>2){
						exit('<p align="center">'.$lang['had_payment'].'</p><p align="center"><a href="'.url('home.php?f=point&a=gotopay&ajax=1&id='.$txnid).'">'.$lang['turn_back'].'</a> <a href="javascript:;" onclick="window.parent.location.reload();">'.$lang['close'].'</a> </p>');
					}
					$txn['subject'] = $lang['buy'].intval($txn['gross']/$_G['point']['recharge']).$lang['point'].'['.$_G['settings']['sitename'].']';
					
					m('payment');
					PaymentModule::payurl($txn,$txn['payment_type']);
				}
				break;
			case 'log':
				
				$limit = 22;
				$links = array();
				$where = '';
				$sql = 'SELECT * FROM '.DB_PREFIX.'point_log WHERE uid='.$_G['uid'].$where;
				$query = $_G['db']->page_query($sql,$limit);
				$num = $_G['db']->result('SELECT count(1) as num FROM '.DB_PREFIX.'point_log  WHERE uid='.$_G['uid'].$where);
				while($data = $_G['db']->fetch_array($query)) {
					$logs[] = $data ;
				}
				$pager = pager($num,$limit);
				break;
			default:
				$orders = $_G['db']->fetch_all("SELECT * FROM ".DB_PREFIX."orders WHERE status < 3 AND type='pointpay' AND uid='".$_G['uid']."'");
				
				$stat = $_G['db']->fetch('user_stat','*',array('uid'=>$_G['uid']));
				
				$status = array(
					'6' => $lang['finished'],
					'2' => $lang['goto_payment'],
					'1' => $lang['goto_payment'],
				);
				
				
			
		}
		include template('uc/point');
	}
}	